package com.nettyAdvanced.p3_line;

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Random;

/**
 * @author： pang-yun
 * @date： 2021-09-15 11:12
 * 功能目的 ：用于   LineBasedFrameDecoder     DeLimiterBasedFrameDecoder
 */

public class LineClient {
    private static final Logger logger = LoggerFactory.getLogger(LineClient.class);

    public static void main(String[] args) {
        send();
        System.out.println("Finish.......");
    }

    //---创建字符串---
    public static StringBuilder makeString(char c, int len) {
        StringBuilder builder = new StringBuilder(len + 2);
        for (int i = 0; i < len; i++) {
            builder.append(c);
        }
        builder.append("\n");
        return builder;
    }

    private static void send() {
        NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup();

        try {
            ChannelFuture channelFuture = new Bootstrap()
                    .group(eventLoopGroup)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<NioSocketChannel>() {
                        @Override
                        protected void initChannel(NioSocketChannel ch) throws Exception {
                            ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                            ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {
                                // 会在连接成功后 触发 active 事件
                                @Override
                                public void channelActive(ChannelHandlerContext ctx) throws Exception {
                                    ByteBuf byteBuf = ctx.alloc().buffer();
                                    char c = '0';
                                    Random random = new Random();
                                    for (int i = 0; i < 10; i++) {
                                        StringBuilder stringBuilder = makeString(c, random.nextInt(256) + 1);
                                        c++;
                                        byteBuf.writeBytes(stringBuilder.toString().getBytes());

                                    }
                                    ctx.writeAndFlush(byteBuf);
                                }
                            });
                        }
                    }).connect(new InetSocketAddress(9999)).sync();
            channelFuture.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            eventLoopGroup.shutdownGracefully();
        }


    }
}
